﻿
@{
    ViewBag.Title = "商品";
}
@section CSS{

}
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            @ViewBag.tName
            <small>商品列表</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
            <li><a href="#">商品管理</a></li>
            <li class="active">@ViewBag.tName 商品列表</li>
        </ol>
    </section>
    <section>
        <div class="col-xs-12">
            <div class="box box-primary ">
                <!-- form start -->
                <form class="form-horizontal">
                    <div class="box-body">
                        <div class="form-group">
                            <label for="title" class="col-sm-2 control-label">商品名称</label>
                            <div class="col-sm-2">
                                <input type="text" class="form-control" id="title" placeholder="商品名称">
                            </div>
                            @*<label for="shopName" class="col-sm-2 control-label">店铺名称</label>
                            <div class="col-sm-2">
                                <input type="text" class="form-control" id="shopName" placeholder="店铺名称">
                            </div>*@
                        </div>
                        <div class="form-group">
                            <label for="code" class="col-sm-2 control-label">商品编号</label>
                            <div class="col-sm-2">
                                <input type="text" class="form-control" id="code" placeholder="商品编号">
                            </div>
                            <label for="category" class="col-sm-2 control-label">类别</label>
                            <div class="col-sm-2">
                                <input type="text" class="form-control" id="category" placeholder="类别">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="inputUName" class="col-sm-2 control-label">商品状态</label>
                            <div class="col-sm-2">
                                <select class="form-control" id="classSelect">
                                    <option value="-1">-请选择状态-</option>
                                    <option value="1">上架</option>
                                    <option value="0">下架</option>
                                </select>
                            </div>
                            <label for="inputUName" class="col-sm-2 control-label">添加日期</label>
                            <div class="col-sm-2">
                                <div class="input-group date">
                                    <div class="input-group-addon">
                                        <i class="fa fa-calendar"></i>
                                    </div>
                                    <input type="text" class="form-control pull-right" id="startTime">
                                </div>
                            </div>
                            <label style="float:left;"> - </label>
                            <div class="col-sm-2">
                                <div class="input-group date">
                                    <div class="input-group-addon">
                                        <i class="fa fa-calendar"></i>
                                    </div>
                                    <input type="text" class="form-control pull-right" id="endTime">
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- /.box-body -->
                    <div class="box-footer ">
                        <button type="button" class="btn btn-primary btn-search center-block" id="btnSearch"><i class="fa fa-search"></i> 查询</button>
                        @*<button type="button" class="btn btn-danger" id="btnDelMul"><i class="fa fa-remove"></i> 批量删除</button>*@
                        @*<button type="button" class="btn btn-success" id="btnExport"><i class="fa fa-download"></i> 导出Excel</button>*@
                    </div>
                    <!-- /.box-footer -->
                </form>
            </div>
        </div>
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="col-xs-12">
                <div class="box">
                    @*<div class="box-header">
                            &nbsp;
                        </div>*@
                    <!-- /.box-header -->
                    <div class="box-body">
                        <table id="DataList" class="table table-bordered table-striped table-hover ">
                            <thead>
                                <tr>
                                    <th>商品编号</th>
                                    <th width="220px">商品名称</th>
                                    <th>商品类别</th>
                                    @*<th>店铺名称</th>*@
                                    <th>@(ViewBag.tid == 2 ? "结算价" : "平台价")</th>
                                    <th>销售量</th>
                                    @*<th style="display:@(ViewBag.tid==2?"none":"block")">服务费 (费率)</th>*@
                                    <th>添加时间</th>
                                    <th>商品状态</th>
                                    @*<th style="display:@(ViewBag.tid==1?"block":"none")">是否新品</th>
                                    <th>是否置顶</th>*@
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody> </tbody>
                        </table>
                    </div>
                    <!-- /.box-body -->
                </div>
                <!-- /.box -->
            </div>
            <!-- /.col -->
        </div>
        <!-- /.row -->
    </section>
    <!-- /.content -->
</div>
<!-- /.content-wrapper -->
@section JS{
<script>

        $(function () {
            //初始化表格
            GenerateTable();
            //鼠标移动到该行变色
            $('.table-hover tbody').on('click', 'tr', function () {
                if ($(this).hasClass('selected')) {
                    $(this).removeClass('selected');
                    console.log();
                }
                else {
                    u_table.$('tr.selected').removeClass('selected');
                    $(this).addClass('selected');
                }
            });
            //搜索事件
            $("#btnSearch").on("click", function () {
                var datemin = $('#startTime').val();
                var datemax = $("#endTime").val();
                if ((datemin == null && datemax != null) || (datemin != null && datemax == null)) {
                    layer.msg('请把时间填充完整!', { icon: 1, time: 1000 });
                }
                else {
                    u_table.fnDestroy();
                    GenerateTable();
                }
            });

            $('#startTime').datepicker({
                autoclose: true
            });

            $('#endTime').datepicker({
                autoclose: true
            });
        });
        var tid=@ViewBag.tid;
        //生成表格
        function GenerateTable() {
            u_table = $('#DataList').dataTable({
                "lengthChange": true,
                "searching": false,
                "ordering": false,
                "iDisplayLength": 10,
                "bProcessing": true,
                "bServerSide": true,
                "fnServerParams": function (aoData) {
                    var classSelect = $("#classSelect").val();
                    var datemin = $('#startTime').val();
                    var datemax = $("#endTime").val();
                    var title = $("#title").val();
                    //var shopName = $("#shopName").val();
                    var code = $("#code").val();
                    var category = $("#category").val();
                    aoData.push(
                        { "name": "dateMin", "value": datemin },
                        { "name": "dateMax", "value": datemax },
                        { "name": "title", "value": title },
                        { "name": "shopName", "value": "" },
                         { "name": "code", "value": code },
                          { "name": "category", "value": category },
                        { "name": "type", "value": tid },
                        { "name": "status", "value": classSelect }
                    );
                },
                "sAjaxSource": "/goods/GoodsList",
                "aoColumns": [
                    { "mDataProp": 'id' },
                     {
                         "mDataProp": 'name', "mRender": function (data, type, full) {
                             //return data.length>6?(data.substring(0,6)+"…") :data;
                             return data;
                         }
                     },
                    { "mDataProp": 'categoryName' },
                    //{ "mDataProp": 'shopName' },
                    { "mDataProp": 'sale_price' },
                    { "mDataProp": 'sale_count' },
                    @*{
                        "mDataProp": 'service_rate', "mRender": function (data, type, full) {
                            return (data*(full.sale_price/100)).toFixed(2)+" ("+full.service_rate+"%)";
                        },
                        "bVisible" :  @(ViewBag.tid == 2 ? "false" : "true"),
                    },*@
                    {
                        "mDataProp": 'add_time', "mRender": function (data, type, full) {
                            return moment(data).format("YYYY-MM-DD HH:mm:ss");
                        }
                    },
                    {
                        "mDataProp": 'published', "mRender": function (data, type, full) {
                            return data==1?"上架":"下架";
                            //if (data == 1) {
                            //    return '<a style="text-decoration:none" class="btn btn-success" onClick="goods_noPass(this,' + full.id + ')" href="javascript:;" title="点击下架">上架</a>';
                            //} else {
                            //    return '<a style="text-decoration:none" class="btn btn-danger" onClick="goods_pass(this,' + full.id + ')" href="javascript:;" title="点击上架">下架</a>';
                            //}
                        }
                    },
                    @*{
                        "mDataProp": 'is_new', "mRender": function (data, type, full) {
                            return data?"新品":"普通";
                        },
                        "bVisible" :  @(ViewBag.tid == 1 ? "true" : "false"),
                    },
                   {
                       "mDataProp": 'is_top', "mRender": function (data, type, full) {
                           return data?"置顶":"常规";
                        }
                    },*@
                    {
                        "mDataProp": 'id', "mRender": function (data, type, full) {
                            return ' <a href = "/goods/EditGoods/' + full.id + '" class="btn btn-default"  title="详情">详情</a> ';
                                //+'| <a  href = "/goods/GoodsPictures/' + full.id + '/0"  title="图片管理">滚动图片</a> |'
                                //+' <a href = "/goods/GoodsPictures/' + full.id + '/1"  title="图片管理">内容图片</a> |'
                                //+ ' <a href = "/goods/GoodsSpec/' + full.id + '"  title="规格管理">规格管理</a> |'
                                //+ ' <a href = "/goods/GoodsSku/' + full.id + '"  title="商品SKU">商品SKU</a> ' ;
                        }
                    }
                ]
            });
        }

        /*上架*/
        function goods_pass(obj, id) {
            layer.confirm('确定上架商品？', function (index) {
                $.ajax({
                    type: "post",
                    url: "/goods/SetGoodsIsUse",
                    dataType: "json",
                    data: { id: id, isUse: 1 },
                    success: function (data) {
                        if (data.status == "y") {
                            $(obj).text("上架").attr("onClick", "goods_noPass(this," + id + ")").attr("title", "点击下架").attr("class", "btn btn-success");
                            layer.msg('已上架', { icon: 6, time: 1000 });
                        }
                        else {
                            layer.msg(data.info, { icon: 5, time: 1200 });
                        }
                    }
                });

            });
        }
        /*下架*/
        function goods_noPass(obj, id) {
            layer.confirm('确定下架商品？', function (index) {
                $.ajax({
                    type: "post",
                    url: "/goods/SetGoodsIsUse",
                    dataType: "json",
                    data: { id: id, isUse: 0 },
                    success: function (data) {
                        if (data.status == "y") {
                            $(obj).text("下架").attr("onClick", "goods_pass(this," + id + ")").attr("title", "点击上架").attr("class", "btn btn-danger");
                            layer.msg('已下架', { icon: 5, time: 1200 });
                        }
                        else {
                            layer.msg(data.info, { icon: 5, time: 1200 });
                        }
                    }
                });

            });
        }

          /*设为新品*/
        function goods_new(obj, id) {
            layer.confirm('确定设为新品？', function (index) {
                $.ajax({
                    type: "post",
                    url: "/goods/SetGoodsIsNew",
                    dataType: "json",
                    data: { id: id, isUse: 1 },
                    success: function (data) {
                        if (data.status == "y") {
                            $(obj).text("新品").attr("onClick", "goods_noNew(this," + id + ")").attr("title", "点击启用").attr("class", "btn btn-warning");
                            layer.msg('已设为新品', { icon: 6, time: 1200 });
                        }
                        else {
                            layer.msg(data.info, { icon: 5, time: 1200 });
                        }
                    }
                });

            });
        }
        /*取消新品*/
        function goods_noNew(obj, id) {
            layer.confirm('确定取消新品设置？', function (index) {
                $.ajax({
                    type: "post",
                    url: "/goods/SetGoodsIsNew",
                    dataType: "json",
                    data: { id: id, isUse: 0 },
                    success: function (data) {
                        if (data.status == "y") {
                            $(obj).text("普通").attr("onClick", "goods_new(this," + id + ")").attr("title", "点击设为新品").attr("class", "btn btn-info");
                            layer.msg('已取消', { icon: 5, time: 1200 });
                        }
                        else {
                            layer.msg(data.info, { icon: 5, time: 1200 });
                        }
                    }
                });

            });
        }

          /*置顶*/
        function goods_top(obj, id) {
            layer.confirm('确定置顶？', function (index) {
                $.ajax({
                    type: "post",
                    url: "/goods/SetGoodsIsTop",
                    dataType: "json",
                    data: { id: id, isUse: 1 },
                    success: function (data) {
                        if (data.status == "y") {
                            $(obj).text("置顶").attr("onClick", "goods_noTop(this," + id + ")").attr("title", "点击取消置顶").attr("class", "btn btn-warning");
                            layer.msg('已置顶', { icon: 6, time: 1000 });
                        }
                        else {
                            alert(data.info);
                        }
                    }
                });

            });
        }
        /*取消置顶*/
        function goods_noTop(obj, id) {
            layer.confirm('确定取消置顶？', function (index) {
                $.ajax({
                    type: "post",
                    url: "/goods/SetGoodsIsTop",
                    dataType: "json",
                    data: { id: id, isUse: 0 },
                    success: function (data) {
                        if (data.status == "y") {
                            $(obj).text("常规").attr("onClick", "goods_top(this," + id + ")").attr("title", "点击置顶").attr("class", "btn btn-info");
                            layer.msg('已取消置顶', { icon: 5, time: 1200 });
                        }
                        else {
                            layer.msg(data.info, { icon: 5, time: 1200 });
                        }
                    }
                });

            });
        }
</script>
}

